package com.ztorn.fiscale.service.impl;

import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;

import com.ztorn.common.core.constant.UserConstants;
import com.ztorn.common.core.utils.DateUtils;
import com.ztorn.common.core.utils.StringUtils;
import com.ztorn.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.ztorn.fiscale.domain.FiscaleOrderItem;
import com.ztorn.fiscale.mapper.FiscaleOrderMapper;
import com.ztorn.fiscale.domain.FiscaleOrder;
import com.ztorn.fiscale.service.IFiscaleOrderService;

/**
 * 订单管理Service业务层处理
 *
 * @author ztorn
 * @date 2025-01-18
 */
@Service
public class FiscaleOrderServiceImpl implements IFiscaleOrderService
{
    @Autowired
    private FiscaleOrderMapper fiscaleOrderMapper;

    /**
     * 查询订单管理
     *
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public FiscaleOrder selectFiscaleOrderByOrderId(Long orderId)
    {
        return fiscaleOrderMapper.selectFiscaleOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     *
     * @param fiscaleOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<FiscaleOrder> selectFiscaleOrderList(FiscaleOrder fiscaleOrder)
    {
        return fiscaleOrderMapper.selectFiscaleOrderList(fiscaleOrder);
    }

    /**
     * 新增订单管理
     *
     * @param fiscaleOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFiscaleOrder(FiscaleOrder fiscaleOrder)
    {
        fiscaleOrder.setCreateTime(DateUtils.getNowDate());
        fiscaleOrder.setCreateBy(SecurityUtils.getUsername());

        // 如果未提供订单号，则自动生成
        if (StringUtils.isEmpty(fiscaleOrder.getOrderNo())) {
            fiscaleOrder.setOrderNo(generateOrderNo());
        }

        int rows = fiscaleOrderMapper.insertFiscaleOrder(fiscaleOrder);
        insertFiscaleOrderItem(fiscaleOrder);
        return rows;
    }

    /**
     * 修改订单管理
     *
     * @param fiscaleOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFiscaleOrder(FiscaleOrder fiscaleOrder)
    {
        fiscaleOrder.setUpdateTime(DateUtils.getNowDate());
        fiscaleOrder.setUpdateBy(SecurityUtils.getUsername());

        fiscaleOrderMapper.deleteFiscaleOrderItemByOrderId(fiscaleOrder.getOrderId());
        insertFiscaleOrderItem(fiscaleOrder);
        return fiscaleOrderMapper.updateFiscaleOrder(fiscaleOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFiscaleOrderByOrderIds(Long[] orderIds)
    {
        fiscaleOrderMapper.deleteFiscaleOrderItemByOrderIds(orderIds);
        return fiscaleOrderMapper.deleteFiscaleOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFiscaleOrderByOrderId(Long orderId)
    {
        fiscaleOrderMapper.deleteFiscaleOrderItemByOrderId(orderId);
        return fiscaleOrderMapper.deleteFiscaleOrderByOrderId(orderId);
    }

    /**
     * 新增订单商品明细信息
     *
     * @param fiscaleOrder 订单管理对象
     */
    public void insertFiscaleOrderItem(FiscaleOrder fiscaleOrder)
    {
        List<FiscaleOrderItem> fiscaleOrderItemList = fiscaleOrder.getOrderItems();
        Long orderId = fiscaleOrder.getOrderId();
        if (StringUtils.isNotNull(fiscaleOrderItemList))
        {
            List<FiscaleOrderItem> list = new ArrayList<FiscaleOrderItem>();
            for (FiscaleOrderItem fiscaleOrderItem : fiscaleOrderItemList)
            {
                fiscaleOrderItem.setOrderId(orderId);
                list.add(fiscaleOrderItem);
            }
            if (list.size() > 0)
            {
                fiscaleOrderMapper.batchFiscaleOrderItem(list);
            }
        }
    }

    /**
     * 根据订单号查询订单
     *
     * @param orderNo 订单号
     * @return 订单信息
     */
    @Override
    public FiscaleOrder selectFiscaleOrderByOrderNo(String orderNo)
    {
        return fiscaleOrderMapper.selectFiscaleOrderByOrderNo(orderNo);
    }

    /**
     * 根据平台订单号查询订单
     *
     * @param platformOrderNo 平台订单号
     * @return 订单信息
     */
    @Override
    public FiscaleOrder selectFiscaleOrderByPlatformOrderNo(String platformOrderNo)
    {
        return fiscaleOrderMapper.selectFiscaleOrderByPlatformOrderNo(platformOrderNo);
    }

    /**
     * 批量更新订单状态
     *
     * @param orderIds 订单ID数组
     * @param orderStatus 订单状态
     * @return 结果
     */
    @Override
    public int batchUpdateOrderStatus(Long[] orderIds, String orderStatus)
    {
        return fiscaleOrderMapper.batchUpdateOrderStatus(orderIds, orderStatus, SecurityUtils.getUsername());
    }

    /**
     * 批量更新报关状态
     *
     * @param orderIds 订单ID数组
     * @param customsStatus 报关状态
     * @return 结果
     */
    @Override
    public int batchUpdateCustomsStatus(Long[] orderIds, String customsStatus)
    {
        return fiscaleOrderMapper.batchUpdateCustomsStatus(orderIds, customsStatus, SecurityUtils.getUsername());
    }

    /**
     * 生成订单号
     *
     * @return 订单号
     */
    @Override
    public String generateOrderNo()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());

        // 生成随机3位数
        int randomNum = (int)(Math.random() * 900) + 100;

        return "ORD" + dateStr + String.format("%03d", randomNum);
    }

    /**
     * 同步平台订单数据
     *
     * @param platformType 平台类型
     * @param platformConfigId 平台配置ID
     * @return 同步结果
     */
    @Override
    public int syncPlatformOrders(String platformType, Long platformConfigId)
    {
        // TODO: 实现平台订单同步逻辑
        // 这里应该调用相应平台的API获取订单数据并保存到数据库
        return 0;
    }

    /**
     * 统计订单数量按状态
     *
     * @return 统计结果
     */
    @Override
    public List<FiscaleOrder> selectOrderCountByStatus()
    {
        return fiscaleOrderMapper.selectOrderCountByStatus();
    }

    /**
     * 统计订单数量按平台
     *
     * @return 统计结果
     */
    @Override
    public List<FiscaleOrder> selectOrderCountByPlatform()
    {
        return fiscaleOrderMapper.selectOrderCountByPlatform();
    }

    /**
     * 校验订单号是否唯一
     *
     * @param orderNo 订单号
     * @param orderId 订单ID（编辑时排除自身）
     * @return 结果
     */
    @Override
    public Boolean checkOrderNoUnique(String orderNo, Long orderId)
    {
        FiscaleOrder order = fiscaleOrderMapper.selectFiscaleOrderByOrderNo(orderNo);
        if (StringUtils.isNotNull(order) && !order.getOrderId().equals(orderId))
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
